<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="com.onlineauction.helpers.Constants"%>
<%@ page import="com.onlineauction.entities.Item"%>
<%@ page import="com.onlineauction.entities.Bid"%>
<%@ page import="com.onlineauction.entities.Auction"%>
<%@ page import="com.onlineauction.helpers.Constants.Category" %>
<%@ page import="com.onlineauction.beans.MakeBidBean"%>
<%@ page import="com.onlineauction.beans.MakeBidBean.MakeBidDataRow"%>
<%@ page import="com.onlineauction.helpers.Constants.Usertype"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- 
	<link rel="stylesheet" href="style.css" />  
-->
<title>Make New Bid</title>
<link rel="stylesheet"
	href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" href="style.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="timepicker_addon.js"></script>
</head>


<jsp:useBean id="makeBidBean"
	class="com.onlineauction.beans.MakeBidBean" scope="request" />
<%
    String itemNameForSearch = request.getParameter(Constants.PRODUCT_NAME);
    makeBidBean.setItemNameForSearch(itemNameForSearch);
    String categoryName = request.getParameter(Constants.CATEGORY);
    makeBidBean.setCategoryName(categoryName);
%>
<%
	// redirect to Home if session is not set (user is not logged in yet)
	String username = (String) session.getAttribute(Constants.USERNAME);
	if (username == null) {
		request.getRequestDispatcher("/Home").forward(request, response);
	} else {
		makeBidBean.setUsername(username);
	}
%>

<body>
	<%
		String message = (String) request.getAttribute(Constants.MESSAGE);
		if (message != null && message.length() != 0) {
			out.println(message);
		}
	%>

	<div class="wrapper">
		<div id="Content">
			<div id="Header">
				<H1>Search for Items</H1>
				<div id="LogOutButtonDiv" align="left">
					<a href="/Home">Home</a> <a href="LogoutAccount">Logout</a>
				</div>
			</div>
			<hr />
			<div id="MainContent">
				<form action="/makeBid.jsp" method="get">
					<table>
						<tr>
							<td align="right">Item name: <input type="text"
								name="<%=Constants.PRODUCT_NAME%>"
								value="<%=makeBidBean.getItemNameForSearch()%>"> (leave blank to see everything)</td>
							<td align="right">Category: <select name=<%=Constants.CATEGORY%> size="1">
									<%
										String allSelect = "";
										if (makeBidBean.getCategory() == null) {
											allSelect = "selected";
										}
									%>
									<option <%=allSelect%> value="<%=Constants.ALL%>"><%=Constants.ALL%></option>
									<%
										Category[] categories = Constants.Category.values();
										for (Category c : categories) {
											String select = "";
											if (c == makeBidBean.getCategory()) {
												select = "selected";
											}
									%>
									<option <%=select%> value="<%=c.toString()%>"><%=c.toString()%></option>
									<%
										}
									%>
							</select></td>
							<td><input type="submit" value="Search" /></td>
						</tr>
					</table>
				</form>

				<h3>Available items:</h3>
				<table>
					<tr>
						<th>Item Name</th>
						<th>Category</th>
						<th>Seller</th>
						<th>Base Price</th>
						<th>Best Price</th>
						<th>Auction Open?</th>
						<th>Action</th>
					</tr>
					<%
						for (MakeBidDataRow row : makeBidBean.getDataRows()) {
					%>
					<tr>
						<td><%=row.itemName%></td>
						<td><%=row.categoryName%></td>
						<td><button><a href=<%="/review?reviewee="+row.username%>><%=row.username%></a></button></td>
						<td><%=row.basePrice%></td>
						<td><%=row.bestPrice%></td>
						<% if (row.auction == null) { %>							
						<td><%=row.auctionOpen%></td>
						<% } else { %>
						<td><button><a href="/ViewAuction?<%=Constants.AUCTION_ID%>=<%=row.auction.getAuctionID()%>"><%=row.auctionOpen%></a></button></td>
						<% } %>						
						<td><button><a
							href="/ConfirmBid?<%=Constants.ITEM_ID%>=<%=row.itemID%>">Bid
								This!</a></button></td>
					</tr>
					<%
						}
					%>
				</table>



			</div>
			<!-- end MainContent -->
		</div>
		<!-- end Content -->
	</div>
	<!-- end wrapper -->
</body>
</html>

